package com.cci.kangdao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cci.kangdao.dao.StationServicePrivilegeTDao;
import com.cci.kangdao.dao.model.CompanyT;
import com.cci.kangdao.dao.model.StationServicePrivilegeT;
import com.cci.kangdao.service.PropertiesUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.dao.CompanyTDao;
import com.cci.kangdao.service.CompanyTService;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.ParameterTool;


//企业类接口实现类
@Service("CompanyTService")
public class CompanyTServiceImpl implements CompanyTService{

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private CompanyTDao companyTDao;
	@Autowired
	private PropertiesUtils propertiesUtils;
	@Resource
	private StationServicePrivilegeTDao stationServicePrivilegeTDao;
	
	//根据服务站id获取所有服务渠道
	@Transactional
	@Override
	public ModelAndView getServiceChannelByServiceStationId(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
		JSONObject obj = new JSONObject();   //保存返给手机端的数据容器
		//判断手机端传来的服务站id是不是空
		String serviceStationId = String.valueOf(map.get("serviceStationID"));
		if("".equals(serviceStationId) || serviceStationId == null) {
			obj.put("data", "");
			obj.put("status", 1);
			obj.put("msg", "服务站id不能为空!");
			ParameterTool.writeResponse(response, obj.toString());	
			return null;
		}
		map.put("serviceStationID", AESTool.decryptString(serviceStationId));  //解密服务站id
		List<Map<String, Object>> companyTList = companyTDao.getServiceChannelByServiceStationId(map);  //根据服务站id获取服务渠道
		if(CollectionUtils.isNotEmpty(companyTList)) {
			List<Map<String, Object>> filterList = new ArrayList<>();
			List<Long> filterCompanyList = stationServicePrivilegeTDao.selectControlledCompanyIdList();
			if(CollectionUtils.isNotEmpty(filterCompanyList)){
				for(Map<String, Object> companyMap:companyTList){
					Long companyId = MapUtils.getLong(companyMap,"id");
					if(filterCompanyList.contains(companyId)){
						String stationNo = MapUtils.getString(companyMap,"BusinessNo");
						StationServicePrivilegeT privilegeT = stationServicePrivilegeTDao.selectByCompanyIdAndServiceStationNo(companyId,stationNo);
						if(null != privilegeT){
							filterList.add(companyMap);
						}
					}else{
						filterList.add(companyMap);
					}
				}
			}else{
				filterList = companyTList;
			}
			if(CollectionUtils.isNotEmpty(filterList)){
				obj.put("status", 0);
				obj.put("msg", "查询成功");
				obj.put("data", new JSONObject().put("companyTList", filterList));
			}else{
				obj.put("status", 1);
				obj.put("msg", "您的服务站没有渠道授权，请联系渠道管理员！");
				obj.put("data", new JSONObject());
			}
		}else {
			obj.put("status", 1);
			obj.put("msg", "您的服务站没有渠道授权，请联系渠道管理员！");
			obj.put("data", "");
		}
		ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public void getOEMCompanyList(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		int page = MapUtils.getInteger(map,"page",1);
		int pageSize = MapUtils.getInteger(map,"pageSize",Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize")));
		PageHelper.startPage(page, pageSize,true);
		String keyword = MapUtils.getString(map,"keyword");
		List<CompanyT> oemCompanyList = companyTDao.getOMECompanyList(keyword);
		PageInfo pageInfo = new PageInfo<>(oemCompanyList);
		Map<String,Object> dataMap = new HashMap<>(3);
		dataMap.put("totalRecord",pageInfo.getTotal());
		List<Map<String,Object>> recordList = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(pageInfo.getList())){
			for(CompanyT companyT:oemCompanyList){
				Map<String,Object> recordMap = new HashMap<>(2);
				recordMap.put("companyId",companyT.getId());
				recordMap.put("companyName",companyT.getCompanyName());
				recordList.add(recordMap);
			}
		}
		if(CollectionUtils.isEmpty(recordList)){
			Map<String,Object> recordMap = new HashMap<>(2);
			recordMap.put("companyId",0);
			recordMap.put("companyName","其他");
			recordList.add(recordMap);
		}
		dataMap.put("recordList", recordList);
		ParameterTool.writeSuccessResponse(response,dataMap);
	}

}
